// [BP] Updated
onWeaponSelect_Check_Limit(response)
{
	restricted = "restricted";
	weapon_class = level.weapons[response].classname;
	weapon_limit = level.weapons[response].limit;
	
	team = self.pers["team"];

	// weapon_class_count = 0;
	weaponlimit_count = 0;
	

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];

		if (!isDefined(player.pers["team"]) || !isDefined(player.pers["weapon"]) )
			continue;

		if (player.pers["team"] != self.pers["team"] || player == self)
			continue;

		playerweapon = player.pers["weapon"];

		if (level.weapons[playerweapon] == level.weapons[response])
			weaponlimit_count++;
		
		//if (level.weapons[playerweapon].classname == weaponclass)
			//weapon_class_count++;
	}

	if (weaponlimit_count >= weapon_limit)
	{
		level.weapons[response].limited = 1;
		
		/* if (team == "allies") 
			level.weaponclass[weaponclass].allies_limited = 1;
		else 
			level.weaponclass[weaponclass].axis_limited = 1;
		*/
		
		// thread Update_WeaponClass_HUDs(team, weaponclass, 0);
		thread Update_One_Weapon_HUDs(level.weapons[response].client_allowcvar, 0);
		return restricted;
	}
	if (weaponlimit_count+1 == weapon_limit)
	{
		level.weapons[response].limited = 1;
		
		/*if (team == "allies") level.weaponclass[weaponclass].allies_limited = 1;
		else level.weaponclass[weaponclass].axis_limited = 1;
		*/
		
		// thread Update_WeaponClass_HUDs(team, weaponclass, 0);
		thread Update_One_Weapon_HUDs(level.weapons[response].client_allowcvar, 0);
		return response;
	}
	else
	{
		level.weapons[response].limited = 0;
		
		/*if (team == "allies") level.weaponclass[weaponclass].allies_limited = 0;
		else level.weaponclass[weaponclass].axis_limited = 0;*/
		
		// thread Update_WeaponClass_HUDs(team, weaponclass, 1);
		thread Update_One_Weapon_HUDs(level.weapons[response].client_allowcvar, 1);
		return response;
	}
}

/*
Update_All_Weapon_Menus()
{
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];

		for(w = 0; w < level.weaponnames.size; w++)
		{
			weaponname = level.weaponnames[w];
			weaponclass = level.weapons[weaponname].classname;
			weaponteam = level.weapons[weaponname].team;

			if (weaponteam == "allies")
			{
				if (level.weaponclass[weaponclass].allies_limited || !level.weapons[weaponname].allow)
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 0);
				else
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 1);
			}

			if (weaponteam == "axis")
			{
				if (level.weaponclass[weaponclass].axis_limited || !level.weapons[weaponname].allow)
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 0);
				else
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 1);
			}
						
			if (level.weapons[weaponname].classname == "shotgun" && player.pers["team"] == "allies")
			{
				if (level.weaponclass["shotgun"].allies_limited || !level.weapons[weaponname].allow)
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 0);
				else
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 1);
			}
			else if (level.weapons[weaponname].classname == "shotgun" && player.pers["team"] == "axis")
			{
				if (level.weaponclass["shotgun"].axis_limited || !level.weapons[weaponname].allow)
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 0);
				else
					player setClientCvar(level.weapons[weaponname].client_allowcvar, 1);
			}
		}
	}
}

Update_All_Weapon_Limits()
{
	// Needs to be called when weapon statuses may have changed:
	// Player Disconnect, Player Changes Teams, Player goes to Spec
	al_sniper_count = 0;
	ax_sniper_count = 0;

	al_boltaction_count = 0;
	ax_boltaction_count = 0;

	al_semiauto_count = 0;
	ax_semiauto_count = 0;

	al_smg_count = 0;
	ax_smg_count = 0;

	al_mg_count = 0;
	ax_mg_count = 0;

	al_shottie_count = 0;
	ax_shottie_count = 0;

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];

		if (!isDefined(player.pers["team"]) || (player.pers["team"] != "axis" || player.pers["team"] != "allies") )
			continue;

		if (!isDefined(player.pers["weapon"]))
			continue;
		spawnweapon = player.pers["weapon"];

		switch (level.weapons[spawnweapon].classname)
		{
		case "sniper":
			if (player.pers["team"] == "axis") ax_sniper_count++;
			else al_sniper_count++;
			break;
		case "boltaction":
			if (player.pers["team"] == "axis") ax_boltaction_count++;
			else al_boltaction_count++;
			break;
		case "semiautomatic":
			if (player.pers["team"] == "axis") ax_semiauto_count++;
			else al_semiauto_count++;
			break;
		case "smg":
			if (player.pers["team"] == "axis") ax_smg_count++;
			else al_smg_count++;
			break;
		case "mg":
			if (player.pers["team"] == "axis") ax_mg_count++;
			else al_mg_count++;
			break;
		case "shotgun":
			if (player.pers["team"] == "axis") ax_shottie_count++;
			else al_shottie_count++;
			break;
		default:
			//println("^3Weapon Limiter: " + spawnweapon + " has no weapon class");
		}
	}

	if (al_sniper_count >= level.weaponclass["sniper"].limit) level.weaponclass["sniper"].allies_limited = 1;
	else level.weaponclass["sniper"].allies_limited = 0;
	if (ax_sniper_count >= level.weaponclass["sniper"].limit) level.weaponclass["sniper"].axis_limited = 1;
	else level.weaponclass["sniper"].axis_limited = 0;

	if (al_boltaction_count >= level.weaponclass["boltaction"].limit) level.weaponclass["boltaction"].allies_limited = 1;
	else level.weaponclass["boltaction"].allies_limited = 0;
	if (ax_boltaction_count >= level.weaponclass["boltaction"].limit) level.weaponclass["boltaction"].axis_limited = 1;
	else level.weaponclass["boltaction"].axis_limited = 0;

	if (al_semiauto_count >= level.weaponclass["semiautomatic"].limit) level.weaponclass["semiautomatic"].allies_limited = 1;
	else level.weaponclass["semiautomatic"].allies_limited = 0;
	if (ax_semiauto_count >= level.weaponclass["semiautomatic"].limit) level.weaponclass["semiautomatic"].axis_limited = 1;
	else level.weaponclass["semiautomatic"].axis_limited = 0;

	if (al_smg_count >= level.weaponclass["smg"].limit) level.weaponclass["smg"].allies_limited = 1;
	else level.weaponclass["smg"].allies_limited = 0;
	if (ax_smg_count >= level.weaponclass["smg"].limit) level.weaponclass["smg"].axis_limited = 1;
	else level.weaponclass["smg"].axis_limited = 0;

	if (al_mg_count >= level.weaponclass["mg"].limit) level.weaponclass["mg"].allies_limited = 1;
	else level.weaponclass["mg"].allies_limited = 0;
	if (ax_mg_count >= level.weaponclass["mg"].limit) level.weaponclass["mg"].axis_limited = 1;
	else level.weaponclass["mg"].axis_limited = 0;

	if (al_shottie_count >= level.weaponclass["shotgun"].limit) level.weaponclass["shotgun"].allies_limited = 1;
	else level.weaponclass["shotgun"].allies_limited = 0;
	if (ax_shottie_count >= level.weaponclass["shotgun"].limit) level.weaponclass["shotgun"].axis_limited = 1;
	else level.weaponclass["shotgun"].axis_limited = 0;

	Update_All_Weapon_Menus();
}*/

// [BP] Heavily Modified
Update_All_Weapon_Limits()
{
	// Needs to be called when weapon statuses may have changed:
	// Player Disconnect, Player Changes Teams, Player goes to Spec
	
	// Might Use this
	Check_WeaponLimit_Server_Changes();

	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];

		if (!isDefined(player.pers["team"]) || (player.pers["team"] != "axis" || player.pers["team"] != "allies") )
			continue;

		if (!isDefined(player.pers["weapon"]))
			continue;
		
		spawnweapon = player.pers["weapon"];
		weapon_class = level.weapons[spawnweapon].classname;
		
		if(weapon_class == "pistol" || weapon_class == "nade" || weapon_class == "smoke")
			continue;
		
		// Increase Count
		level.weapons[spawnweapon].count++;
		
		if (level.allow_secondary && isDefined(player.pers["secondary_weapon"]))
		{
			secondary = player.pers["secondary_weapon"];
			weapon_class = level.weapons[secondary].classname;
			
			if(weapon_class == "pistol" || weapon_class == "nade" || weapon_class == "smoke")
				continue;
	
			level.weapons[secondary].count++;
		}
	}
	
	for(i = 0; i < level.weaponnames.size; i++)
	{
		wpname = level.weaponnames[i];
		weapon_class = level.weapons[wpname].classname;
		if(weapon_class == "pistol" || weapon_class == "nade" || weapon_class == "smoke")
			continue;
			
		was_it_limited = level.weapons[wpname].limited;
		
		if(level.weapons[wpname].count >= level.weapons[wpname].limit)
			level.weapons[wpname].limited = 1;
		else
			level.weapons[wpname].limited = 0;
			
		if(level.weapons[wpname].limited != was_it_limited)
		{
			allowed = 1;
			
			if(level.weapons[wpname].limited)
				allowed = 0;
				
			thread Update_One_Weapon_HUDs(level.weapons[wpname].client_allowcvar, allowed);
		}
		
		// Reset the Counts
		level.weapons[wpname].count = 0;
	}
}

Check_WeaponLimit_Server_Changes()
{
	level.weapons["greasegun_mp"].limit				= [[level.setdvar]]("bp_greasegun_limit", 50, 0, 50);
	level.weapons["m1carbine_mp"].limit				= [[level.setdvar]]("bp_m1carbine_limit", 50, 0, 50);
	level.weapons["m1garand_mp"].limit				= [[level.setdvar]]("bp_m1garand_limit", 50, 0, 50);
	level.weapons["springfield_mp"].limit			= [[level.setdvar]]("bp_springfield_limit", 50, 0, 50);
	level.weapons["unscoped_springfield_mp"].limit	= [[level.setdvar]]("bp_unscoped_springfield_limit", 50, 0, 50);
	level.weapons["thompson_mp"].limit				= [[level.setdvar]]("bp_thompson_limit", 50, 0, 50);
	level.weapons["bar_mp"].limit					= [[level.setdvar]]("bp_bar_limit", 50, 0, 50);
	level.weapons["sten_mp"].limit					= [[level.setdvar]]("bp_sten_limit", 50, 0, 50);
	level.weapons["enfield_mp"].limit				= [[level.setdvar]]("bp_enfield_limit", 50, 0, 50);
	level.weapons["enfield_scope_mp"].limit			= [[level.setdvar]]("bp_enfield_scope_limit", 50, 0, 50);
	level.weapons["bren_mp"].limit					= [[level.setdvar]]("bp_bren_limit", 50, 0, 50);
	level.weapons["PPS42_mp"].limit					= [[level.setdvar]]("bp_PPS42_limit", 50, 0, 50);
	level.weapons["mosin_nagant_mp"].limit			= [[level.setdvar]]("bp_mosin_nagant_limit", 50, 0, 50);
	level.weapons["SVT40_mp"].limit					= [[level.setdvar]]("bp_SVT40_limit", 50, 0, 50);
	level.weapons["mosin_nagant_sniper_mp"].limit	= [[level.setdvar]]("bp_mosin_nagant_sniper_limit", 50, 0, 50);
	level.weapons["ppsh_mp"].limit					= [[level.setdvar]]("bp_ppsh_limit", 50, 0, 50);
	level.weapons["mp40_mp"].limit					= [[level.setdvar]]("bp_mp40_limit", 50, 0, 50);
	level.weapons["kar98k_mp"].limit				= [[level.setdvar]]("bp_kar98k_limit", 50, 0, 50);
	level.weapons["g43_mp"].limit					= [[level.setdvar]]("bp_g43_limit", 50, 0, 50);
	level.weapons["kar98k_sniper_mp"].limit			= [[level.setdvar]]("bp_kar98k_sniper_limit", 25, 0, 50);
	level.weapons["mp44_mp"].limit					= [[level.setdvar]]("bp_mp44_limit", 50, 0, 50);
	level.weapons["shotgun_mp"].limit				= [[level.setdvar]]("bp_shotgun_limit", 50, 0, 50);
	level.weapons["axis_shotgun_mp"].limit			= [[level.setdvar]]("bp_axis_shotgun_limit", 0, 0, 50);
	level.weapons["axis_sf_shotgun_mp"].limit		= [[level.setdvar]]("bp_axis_sf_shotgun_limit", 0, 0, 50);
	level.weapons["allied_shotgun_mp"].limit		= [[level.setdvar]]("bp_allied_shotgun_limit", 0, 0, 50);
	level.weapons["allied_sf_shotgun_mp"].limit		= [[level.setdvar]]("bp_allied_sf_shotgun_limit", 0, 0, 50);
}


// Not Used
Update_WeaponClass_HUDs(team, weaponclass, allowed)
{
	if (!isDefined(allowed))
	{
		iprintln("Error: Allowed not sent to Update_WeaponClass_HUDs()");
		return;
	}

	for(weaponname = 0; weaponname < level.weaponnames.size; weaponname++)
	{
		name = level.weaponnames[weaponname];
		if (level.weapons[name].classname != weaponclass)
			continue;

		/* if (weaponclass == "shotgun")
		{
			thread Update_Shotgun_Weapon_HUDs(team);
			return;
		} */

		if (level.weapons[name].team != team)
			continue;

		thread Update_One_Weapon_HUDs(level.weapons[name].client_allowcvar, allowed);
		wait level.fps_multiplier * 0.05;
	}
}

// Not Used
Update_Shotgun_Weapon_HUDs(team)
{
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];

		if (!isDefined(player.pers["team"]))
			continue;

		if (player.pers["team"] != team)
			continue;

		if (team == "allies")
		{
			if(level.weaponclass["shotgun"].allies_limited)
				player setClientCvar(level.weapons["shotgun_mp"].client_allowcvar, 0);
			else
				player setClientCvar(level.weapons["shotgun_mp"].client_allowcvar, 1);
		}
		else if (team == "axis")
		{
			if(level.weaponclass["shotgun"].axis_limited)
				player setClientCvar(level.weapons["shotgun_mp"].client_allowcvar, 0);
			else
				player setClientCvar(level.weapons["shotgun_mp"].client_allowcvar, 1);
		}
	}
}

Update_One_Weapon_HUDs(client_dvar, allowed)
{
	players = getentarray("player", "classname");
	for(i = 0; i < players.size; i++)
	{
		player = players[i];

		if (allowed)
			player setClientCvar(client_dvar, 1);
		else
			player setClientCvar(client_dvar, 0);
	}
}